package com.indexingsystem.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.DelExportThemeWordsReqVo;
import com.indexingsystem.boss.Vo.ExportThemeWordsListReqVo;
import com.indexingsystem.boss.Vo.IdxThemeWordsExportDeleVo;
import com.indexingsystem.boss.admin.mapper.IdxExportThemeWordsMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.ExportThemeWordsListResVo;
import com.indexingsystem.boss.entity.IdxExportIndexingProduct;
import com.indexingsystem.boss.entity.IdxExportThemeWords;
import com.indexingsystem.boss.entity.IdxExportThemeWordsExample;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.exception.ServiceResultException;
import com.indexingsystem.system.service.IdxExportThemeWordsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class IdxExportThemeWordsServiceImpl implements IdxExportThemeWordsService {


    @Resource
    private IdxExportThemeWordsMapper idxExportThemeWordsMapper;
    @Override
    public int deleteByPrimaryKey(String id) {
        return idxExportThemeWordsMapper.deleteByPrimaryKey( id );
    }

    @Override
    public int insert(IdxExportIndexingProduct record) {
        return idxExportThemeWordsMapper.insert( record );
    }

    @Override
    public int insertSelective(IdxExportIndexingProduct record) {
        return idxExportThemeWordsMapper.insertSelective( record );
    }

    @Override
    public IdxExportIndexingProduct selectByPrimaryKey(String id) {
        return idxExportThemeWordsMapper.selectByPrimaryKey( id );
    }

    @Override
    public int updateByPrimaryKeySelective(IdxExportIndexingProduct record) {
        return idxExportThemeWordsMapper.updateByPrimaryKeySelective( record );
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(IdxExportIndexingProduct record) {
        return idxExportThemeWordsMapper.updateByPrimaryKeyWithBLOBs( record );
    }

    @Override
    public int updateByPrimaryKey(IdxExportIndexingProduct record) {
        return idxExportThemeWordsMapper.updateByPrimaryKey( record );
    }

    @Override
    public List<IdxExportThemeWords> selectIdxExportThemeById(Integer themeWordsId) {
        return idxExportThemeWordsMapper.selectIdxExportThemeById(themeWordsId);
    }

	@Override
	public Integer deleteThemeWordsExport(IdxThemeWordsExportDeleVo idxThemeWordsExportDeleVo) {
		return idxExportThemeWordsMapper.deleteThemeWordsExport( idxThemeWordsExportDeleVo );
	}

    @Override
    public Integer deleteThemeWordsExport(DelExportThemeWordsReqVo reqVo) throws ServiceResultException{
        IdxThemeWordsExportDeleVo deleVo = new IdxThemeWordsExportDeleVo();
        deleVo.setIds(reqVo.getIds());
        return idxExportThemeWordsMapper.deleteThemeWordsExport(deleVo);
    }

    @Override
    public PagedResult<ExportThemeWordsListResVo> getExportThemeWordsList(ExportThemeWordsListReqVo reqVo) throws ParseException {
        Integer pageNo = reqVo.getPageNo() == null ? 1 : reqVo.getPageNo();
        Integer pageSize = reqVo.getPageSize() == null ? 20 : reqVo.getPageSize();
        IdxExportThemeWordsExample example = new IdxExportThemeWordsExample();
        IdxExportThemeWordsExample.Criteria criteria = example.createCriteria();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str1 = reqVo.getStartTime();
        String str2 = reqVo.getEndTime();
        String queryName = reqVo.getQueryCriteria();
        Date value1 = new Date();
        Date value2 = new Date();
        if (reqVo.getThemeTableId() != null) {
            criteria.andThemeTableIdEqualTo(reqVo.getThemeTableId());
        }
        if (reqVo.getThemeWordsId() != null) {
            criteria.andThemeWordsIdEqualTo(reqVo.getThemeWordsId());
        }
        if (StringUtils.isNotBlank(str1)) {
            value1 = df.parse(str1);
            criteria.andCreateTimeGreaterThanOrEqualTo(value1);
        }
        if (StringUtils.isNotBlank(str2)) {
            value2 = df.parse(str2);
            criteria.andCreateTimeLessThanOrEqualTo(value2);
        }
        if (StringUtils.isNotBlank(queryName)) {
            criteria.andThemeWordsNameLike("%" + queryName + "%");
        }
        if (StringUtils.isNotBlank(reqVo.getExportType())) {
            criteria.andExportTypeEqualTo(reqVo.getExportType());
        }

        example.getOredCriteria().add(criteria);
        //startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        PageHelper.startPage(pageNo,pageSize);
        return BeanUtils.toPagedResult(idxExportThemeWordsMapper.selectByExample(example));
    }

    @Override
    public int selectCountByIds(List<String> ids) {
        return idxExportThemeWordsMapper.selectCountByIds(ids);
    }

}
